<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
//商品分类控制器
class GcateController extends CommonController{
	private $gcate_model;
	private $form_model;
	public function __init()
	{
		//调用CommonController里的构造函数
		parent::__init();
		//实例化gcate模型
		$this->gcate_model = new \Common\Model\Gcate;
		//实例化form模型
		$this->form_model = new \Common\Model\Form;
	}
	
	public function index(){
		//分页
/*		$total = $this->gcate_model->count();
		$page = Page::row(12)->make($total);
		View::with('page',$page);*/
		//获得数据
//		$data = $this->gcate_model->leftjoin('form','form_fid','=','fid')->limit(Page::limit())->get();
		$data = $this->gcate_model->get();
		$data = Data::tree($data,'cate_name','cate_id','p_id');
		//p($data);exit;
		View::with('data',$data);
		View::make();
	}
	//添加
	public function add(){
		//获取类型ID
		$fid = Q('get.fid',0,'intval');
		
		if(IS_POST){
			if(!$this->gcate_model->store()) View::error($this->gcate_model->getError());
			View::success('添加成功',U('index'));
		}
		
		//获得所属类型
		$formData = $this->form_model->get();
		View::with('formData',$formData);
		//获得所属商品分类
		$gcateData = $this->gcate_model->get();
		$gcateData = Data::tree($gcateData,'cate_name','cate_id','p_id');
		View::with('gcateData',$gcateData);
		
		View::make();
	}
	//添加子分类
	public function addSon(){
		//获取类型ID
		$fid = Q('get.fid',0,'intval');
		
		if(IS_POST){
		    if($this->gcate_model->store()) View::success('添加成功',U('index'));
			View::error($this->gcate_model->getError());
		}
		
		$cate_id = Q('get.cate_id',0,'intval');
		$parent = $this->gcate_model->where("cate_id={$cate_id}")->field('cate_id,cate_name')->find();
		View::with('parent',$parent);
		
		View::make();
	}
	//编辑
	public function edit(){
		//获取类型ID
		$fid = Q('get.fid',0,'intval');
		
		//2.修改
		if(IS_POST){
			if($this->gcate_model->edit()) View::success('修改成功',U('index'));
			View::error($this->gcate_model->getError());
		}

		//1.获取旧数据
		$cate_id = Q('get.cate_id',0,'intval');
		$oldData  = $this->gcate_model->where("cate_id={$cate_id}")->find();
		View::with('oldData',$oldData);
		
		//所属类型
		$formData = $this->form_model->get();
		View::with('formData',$formData);
		
		//处理“所属分类”不是是自己和子集
		$gcataData = $this->gcate_model->getNoMy($cate_id);
		View::with('gcataData',$gcataData);
		
		View::make();
	}
	//删除
	public function del(){
		$cate_id = Q('get.cate_id',0,'intval');
		
		$p_id = $this->gcate_model->where("cate_id={$cate_id}")->pluck('p_id');
		
		$this->gcate_model->where("p_id={$cate_id}")->save(array('p_id'=>$p_id));
		$this->gcate_model->where("cate_id={$cate_id}")->delete();
		
		View::success('删除成功',U('index'));
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

 ?>